import scrapy


class RequestGame520Spider(scrapy.Spider):
    name = "request_game520"

    async def start(self):
        yield scrapy.Request('https://www.gamer520.com/page/1')

    async def parse(self, response):
        blocks = response.xpath('//h2[@class="entry-title"]/a')
        next_page = response.xpath('//a[@class="next page-numbers"]/@href').get()
        for block in blocks:
            url = block.xpath('@href').get()
            title = block.xpath('text()').get()
            print(title, url)
            yield scrapy.Request(url, callback=self.parse_detail, cb_kwargs={'title': title})

        if next_page:
            yield response.follow(next_page, callback=self.parse)
        else:
            print('爬完了')

    async def parse_detail(self, response, title):
        img_url = response.xpath('//div[@class="entry-content u-text-format u-clearfix"]/p[1]/img/@src').get()
        yield scrapy.Request(img_url, callback=self.parse_img, cb_kwargs={'title': title})

    async def parse_img(self, response, title):
        data = response.body

        with open(f'./static/{title}.jpg', 'wb') as f:
            f.write(data)
